/*****************************/
/****** 上传设置助手 *********/
/***** foxchen 20161107 ******/
/*** 基于jquery+uploadify ****/
/*****************************/

var uploadHelper = function(option){
	this._file =         option.file || '#file_upload';//上传绑定文件组件
	this._target =       option.target || '#main';//返回结果容器
	this._swf =          option.swf || 'uploadify.swf';//上传swf地址
	this._uploader =     option.uploader || 'http://192.168.1.119:8080/zdemo/file/upload.ajax';//上传ajax路径
	this._uri =          option.uri || 'http://192.168.1.119:8080/zdemo';//图片路径补全
	this._marginRight =  option.marginRight || 0;//右间距
	this._marginLeft =   option.marginLeft || 0;//左间距
	this._marginTop =    option.marginTop || 0;//上间距
	this._marginBottom = option.marginBottom || 0;//下间距
	this._isBlock      = option.isBlock || false;//是否显示为块状化
	this._width =        option.width || '100px';//小块宽度
	this._height =       option.height || 'auto';//小块高度
	this._conWidth =     option.conWidth || '100%';//容器宽度
	this._drag =         option.drag || true;//是否启用拖动
	this._float =        option.float || 'left';//元素浮动
};

//初始化
uploadHelper.prototype.init = function() {
	var _o = this;
	var _t = '';
	_t += '<style type="text/css">.ui-sortable-placeholder.ui-state-highlight{float: left;border:none;width: ' + _o._width + ';}</style>';
	_t += '<ul id="li_list" style="width:' + _o._conWidth + ';overflow:hidden;"></ul>';
	$(_o._target).html(_t);
};

//上传图片序列
uploadHelper.prototype.upload = function(){
	var _o = this;
	_o.init();
	$(_o._file).uploadify({
		'buttonText' : '上传图片',
        'swf'      : _o._swf,
        'uploader' : _o._uploader,
        onUploadSuccess:function(file,data,response){
        	var ops = '';
     		ops += 'margin-right:' + _o._marginRight + 'px;margin-left:' + _o._marginLeft + 'px;margin-top:' + _o._marginTop + 'px;margin-bottom:' + _o._marginBottom + 'px;';
            var s = '<li data-url="' + (_o._uri + JSON.parse(data).d.savePath) + '" style="overflow:hidden;width:' + _o._width +';height:' + _o._height + ';float:' + _o._float + ';' + ops + '"><img style="' + (_o._isBlock ? 'display:block;' : 'display:inline;') + '" src="' + (_o._uri + JSON.parse(data).d.savePath) + '"/></li>';
            $('#li_list').append(s);
            if(_o._drag){
            	$('#li_list').sortable({
            		'placeholder':'ui-state-highlight'
            	});
            	$('#li_list').disableSelection();
            };
        }
    });
};

// 返回图片结构序列
uploadHelper.prototype.picList = function(list){
	var _o = this,
		s = '';
	var ops = '';
	ops += 'margin-right:' + _o._marginRight + 'px;margin-left:' + _o._marginLeft + 'px;margin-top:' + _o._marginTop + 'px;margin-bottom:' + _o._marginBottom + 'px;';
    s = '<li data-url="' + (_o._uri + JSON.parse(data).d.savePath) + '" style="overflow:hidden;width:' + _o._width +';height:' + _o._height + ';float:' + _o._float + ';' + ops + '"><img style="' + (_o._isBlock ? 'display:block;' : 'display:inline;') + '" src="' + (_o._uri + JSON.parse(data).d.savePath) + '"/></li>';
};


